package com.pucheng.bracelet.data.bean;

import com.blankj.utilcode.util.LogUtils;
import com.pucheng.bracelet.app.util.ByteUtils;
import com.pucheng.bracelet.app.util.DatetimeUtil;

import java.util.ArrayList;
import java.util.List;

public class DataUtils {
    public static List<SleepBean> getSleeps(byte[] data) {
        int startInde = 4;
        byte[] bArr = ByteUtils.hexStringToByte(ByteUtils.byteArrToHexString(data).replaceAll("ff", ""));
        int count = ByteUtils.highLowExchangeToInt(ByteUtils.subBytes(bArr, 2, 2));
        LogUtils.e(String.format("共%s条记录", count));  //
        int[] lengArr = new int[count];
        int lastLen = 0;
        List<SleepBean> sleepDatas = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int leng = ByteUtils.highLowExchangeToInt(ByteUtils.subBytes(bArr, startInde + 2 * count + (i * 2), 2));
            lengArr[i] = leng;
            LogUtils.e(String.format("每条记录长度：%s", leng));
            int sleepIndex = startInde + (2 + 2 + 4) * count + lastLen;
            LogUtils.e("睡眠数据开始下标：" + sleepIndex);
            if (bArr.length>= sleepIndex+leng) {
                byte[] sleepData = ByteUtils.subBytes(bArr, sleepIndex, leng);
                int timestamp = ByteUtils.littleEndianToInt(ByteUtils.subBytes(sleepData, 0, 4));
                LogUtils.e("timestamp=" + timestamp);
                int nodeCount = ByteUtils.littleEndianToInt(ByteUtils.subBytes(sleepData, 4, 4));
                LogUtils.e("睡眠变化次数" + nodeCount);
                SleepBean sleepBean = new SleepBean();
                sleepBean.setTimestamp(timestamp);
                sleepBean.setDate(DatetimeUtil.formatDate(timestamp, DatetimeUtil.DATE_PATTERN));
                int minOffset = 0;
                int lightTime = 0;
                int restfulTime = 0;
                int soberTime = 0;
                int activeTime = 0;
                List<SleepNode> sleepNodes = new ArrayList<>();
                int lastOffset = 0;
                for (int i1 = 0; i1 < nodeCount; i1++) {
                    SleepNode bean = new SleepNode();
                    bean.setStatus(ByteUtils.littleEndianToInt(ByteUtils.subBytes(sleepData, i1 * 4 + 10, 2)));
                    bean.setMin_offset(ByteUtils.littleEndianToInt(ByteUtils.subBytes(sleepData, i1 * 4 + 8, 2)));
                    if (bean.getStatus() == 0) {
                        soberTime += (bean.getMin_offset() - lastOffset);
                    }else if (bean.getStatus() == 1) {
                        lightTime += (bean.getMin_offset() - lastOffset);
                    }else if (bean.getStatus() == 2) {
                        restfulTime += (bean.getMin_offset() - lastOffset);
                    }else {
                        activeTime += (bean.getMin_offset() - lastOffset);
                    }
                    lastOffset = bean.getMin_offset();
                    sleepNodes.add(bean);
                }
                LogUtils.e("minOffset = " + minOffset);
                sleepBean.setLightTime(lightTime);
                sleepBean.setActiveTime(activeTime);
                sleepBean.setRestfulTime(restfulTime);
                sleepBean.setSoberTime(soberTime);
                sleepBean.setTotalTime(soberTime+lightTime+restfulTime+activeTime);
                sleepBean.setNodes(sleepNodes);
                lastLen += leng;
                sleepDatas.add(sleepBean);
            }
        }
        return sleepDatas;
    }

    public static List<AllHertRateBean> getHeartRateData(byte[] data) {
        int startInde = 4;
        byte[] bArr = ByteUtils.hexStringToByte(ByteUtils.byteArrToHexString(data).replaceAll("ff", ""));
        int count = ByteUtils.highLowExchangeToInt(ByteUtils.subBytes(bArr, 2, 2));
        LogUtils.e(String.format("共%s条记录", count));  //
        int[] lengArr = new int[count];
        int lastLen = 0;
        List<AllHertRateBean> hertRateDatas = new ArrayList<>();
        for (int i = 0; i < count; i++) {
            int leng = ByteUtils.highLowExchangeToInt(ByteUtils.subBytes(bArr, startInde + 2 * count + (i * 2), 2));
            lengArr[i] = leng;
            LogUtils.e(String.format("每条记录长度：%s", leng));
            int sleepIndex = startInde + (2 + 2 + 4) * count + lastLen;
            LogUtils.e("心率数据开始下标：" + sleepIndex);
            if (bArr.length>= sleepIndex+leng) {
                byte[] sleepData = ByteUtils.subBytes(bArr, sleepIndex, leng);
                int timestamp = ByteUtils.littleEndianToInt(ByteUtils.subBytes(sleepData, 0, 4));
                LogUtils.e("timestamp=" + timestamp);
                int nodeCount = ByteUtils.littleEndianToInt(ByteUtils.subBytes(sleepData, 4, 4));
                LogUtils.e("心率变化次数" + nodeCount);
                AllHertRateBean sleepBean = new AllHertRateBean();
                sleepBean.setTimestamp(timestamp);
                sleepBean.setDate(DatetimeUtil.formatDate(timestamp, DatetimeUtil.DATE_PATTERN));
                int minOffset = 0;
                List<HertRateNode> sleepNodes = new ArrayList<>();
                int totalBpm = 0;
                if (nodeCount>0) {
                    for (int i1 = 0; i1 < nodeCount; i1++) {
                        HertRateNode bean = new HertRateNode();
                        bean.setBpm(ByteUtils.littleEndianToInt(ByteUtils.subBytes(sleepData, i1 * 4 + 10, 2)));
                        bean.setMin_offset(ByteUtils.littleEndianToInt(ByteUtils.subBytes(sleepData, i1 * 4 + 8, 2)));
                        totalBpm+=bean.getBpm();
    //                    lastOffset = bean.getMin_offset();
                        sleepNodes.add(bean);
                    }
                    LogUtils.e("minOffset = " + minOffset);
                    sleepBean.setAverageHR(totalBpm/nodeCount);
                }
                sleepBean.setNodes(sleepNodes);
                lastLen += leng;
                hertRateDatas.add(sleepBean);
            }
        }
        return hertRateDatas;
    }
}
